package app.project.streetlensproject.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import app.project.streetlensproject.HomeActivity;
import app.project.streetlensproject.R;
import app.project.streetlensproject.adapter.CategoryAdapter;
import app.project.streetlensproject.model.Category;
import app.project.streetlensproject.serverapi.ServerAPI;
import app.project.streetlensproject.util.AsyncTask;
import app.project.streetlensproject.util.CommonUtils;
import app.project.streetlensproject.util.Constants;
import app.project.streetlensproject.util.DialogUtils;
import app.project.streetlensproject.util.GetDataTask;
import app.project.streetlensproject.util.IGetDataTask;

public class FgEditUser extends Fragment implements OnClickListener {
	private Button btnOk, btnCancel;
	private EditText edtOldPass, edtNewPass1, edtNewPass2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fg_edit_user, container, false);
		btnOk = (Button) v.findViewById(R.id.btn_ok);
		btnCancel = (Button) v.findViewById(R.id.btn_cancel);
		edtNewPass1 = (EditText) v.findViewById(R.id.edt_new_pass1);
		edtNewPass2 = (EditText) v.findViewById(R.id.edt_new_pass2);
		edtOldPass = (EditText) v.findViewById(R.id.edt_old_pass);

		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		return v;
	}

	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_ok) {
			if (!edtNewPass1.getText().toString().trim().equals(edtNewPass2.getText().toString().trim())) {
				Toast.makeText(getActivity(), R.string.new_pass_not_match, Toast.LENGTH_LONG).show();
				return;
			}
			new GetDataTask(getActivity(), new IGetDataTask() {
				@Override
				public void onPostExecute(boolean result) throws Exception {
					super.onPostExecute(result);
					if (result) {
						((HomeActivity) getActivity()).gotoCategory();
						Toast.makeText(getActivity(), R.string.change_pass_sucess, Toast.LENGTH_LONG).show();
					} else
						Toast.makeText(getActivity(), R.string.has_error, Toast.LENGTH_LONG).show();
				}

				@Override
				public boolean doInBackGround() throws Exception {
					String userName = CommonUtils.getStringSetting(getActivity(), Constants.USER_NAME, null);
					String tokenId = CommonUtils.getStringSetting(getActivity(), Constants.TOKEN_ID, null);
					return ServerAPI.ChangePassword(tokenId, userName, edtOldPass.getText().toString().trim(),
							edtNewPass1.getText().toString().trim());
				}
			}, true).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		} else if (id == R.id.btn_cancel) {
			((HomeActivity) getActivity()).gotoCategory();
		}
	}
}
